<?php
namespace app\admin\controller;
use think\Controller;
use think\Session;
use think\Db;
use think\Request;
class Auth extends Controller{
	public function __construct(){
		parent::__construct();
		$controller = strtolower(request()->controller());
		$action = strtolower(request()->action());
		if($controller != 'auth' && $controller != 'index'){
			if(!Session::has('admin')){
				$this->redirect('admin/auth/login');
				exit;
			}
		}
		$this->assign('controller',$controller);
	}
	public function login(){
		return $this->fetch();
	}
	public function dologin(){
		$username = input('post.username');
		$pwd = input('post.pwd');
		if(empty($username) || empty($pwd)){
			$this->error('登录失败');
			exit;
		}
		$where['username'] = array('EQ',$username);
		$userinfo = Db::name('user_admin')->where($where)->find();
		if(empty($userinfo)){
			$this->error('该管理员不存在');
			exit;
		}
		if(strcmp($userinfo['pwd'], md5($pwd)) != 0){
			$this->error('密码错误');
			exit;
		}
		Session::set('admin',$userinfo);
		$this->redirect('admin/index/index');
	}
	public function loginout(){
		Session::clear();
		$this->redirect('admin/auth/login');
	}
	/**
	*检测参数
	*
	*/
	public function check_param($arr,$data){
		if(empty($data)){
			return false;
		}
		foreach ($arr as $key => $value) {
			if(!array_key_exists($value, $data) || empty($data[$value])){
				return false;
			}
		}
		return true;
	}
}